package com.hrms.utils;

import com.github.pagehelper.PageHelper;
import com.hrms.domain.web.PageInfo;

/**
 * 分页工具类
 * 
 * @author ruoyi
 */
public class PageUtils extends PageHelper
{
    /**
     * 当前记录起始索引
     */
    public static final String PAGE_NUM = "pageNum";

    /**
     * 每页显示记录数
     */
    public static final String PAGE_SIZE = "pageSize";

    /**
     * 排序列
     */
    public static final String ORDER_BY_COLUMN = "orderByColumn";

    /**
     * 排序的方向 "desc" 或者 "asc".
     */
    public static final String IS_ASC = "isAsc";

    /**
     * 分页参数合理化
     */
    public static final String REASONABLE = "reasonable";

    /**
     * 封装分页对象
     */
    public static PageInfo getPageDomain()
    {
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageNum(ConvertUtils.toInt(ServletUtils.getParameter(PAGE_NUM), 1));
        pageInfo.setPageSize(ConvertUtils.toInt(ServletUtils.getParameter(PAGE_SIZE), 10));
        pageInfo.setOrderByColumn(ServletUtils.getParameter(ORDER_BY_COLUMN));
        pageInfo.setIsAsc(ServletUtils.getParameter(IS_ASC));
        pageInfo.setReasonable(ServletUtils.getParameterToBool(REASONABLE));
        return pageInfo;
    }

    public static PageInfo buildPageRequest()
    {
        return getPageDomain();
    }


    /**
     * 设置请求分页数据
     */
    public static void startPage()
    {
        PageInfo pageInfo = buildPageRequest();
        Integer pageNum = pageInfo.getPageNum();
        Integer pageSize = pageInfo.getPageSize();
        String orderBy = pageInfo.getOrderBy();
        Boolean reasonable = pageInfo.getReasonable();
        PageHelper.startPage(pageNum, pageSize, orderBy).setReasonable(reasonable);
    }

    /**
     * 清理分页的线程变量
     */
    public static void clearPage()
    {
        PageHelper.clearPage();
    }
}
